package com.ag.lwc.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ag.bean.TbUrealGroup;
import com.ag.dao.TbEventCleanupDAO;

/**
 * 虚拟组的dao
 * @author user
 *
 */
public class ContactGroupDAO extends TbEventCleanupDAO{
	
	/**
	 * 查询某个用户的所有虚拟组
	 * @param userId
	 * @return
	 */
	public List<TbUrealGroup> findAll(String userId)
	{
		List<TbUrealGroup> list=null;
		Session session = this.getSessionFactory().openSession();
		list = session.createQuery("from TbUrealGroup where user.UId :=userId").setString("userId", userId).list();
		session.close();
		return list;
	}
	
	/**
	 * 根据虚拟组的id查询虚拟组
	 * @param groupId
	 * @return
	 */
	public TbUrealGroup findOneGroup(int groupId)
	{
		Session session = this.getSessionFactory().openSession();
		TbUrealGroup group = (TbUrealGroup) session.get(TbUrealGroup.class, groupId);
		session.close();
		return group;
	}
	/**
	 * 修改虚拟组
	 * @param group
	 */
	public void updateGroup(TbUrealGroup group)
	{
		if(group==null||group.getGroupId()==0) return ;
		Session session = this.getSessionFactory().openSession();
		Transaction tx=null;
		try {
			tx = session.beginTransaction();
			session.update(group);
			tx.commit();
		} catch (HibernateException e) {
			tx.rollback();
			e.printStackTrace();
		}
		
		session.close();
	}
	/**
	 * 根据id删除虚拟组
	 * @param groupId
	 */
	public void deleteGroup(int groupId)
	{
		if(groupId==0) return;
		Session session = this.getSessionFactory().openSession();
		Transaction tx=null;
		TbUrealGroup group = (TbUrealGroup) session.get(TbUrealGroup.class, groupId);
		if(group==null) return;
		try {			
			tx = session.beginTransaction();
			session.delete(group);
			tx.commit();
		} catch (HibernateException e) {
			tx.rollback();
			e.printStackTrace();
		}
		
		session.close();
	}
	/**
	 * 增加虚拟组
	 * @param group
	 */
	public void addGroup(TbUrealGroup group)
	{
		if(group==null) return;
		Session session = this.getSessionFactory().openSession();
		Transaction tx=null;
		try {			
			tx = session.beginTransaction();
			session.save(group);
			tx.commit();
		} catch (HibernateException e) {
			tx.rollback();
			e.printStackTrace();
		}
		
		session.close();
	}

}
